import requests
import re
import json
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                  ' Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.53'
}
# 动态页面参数字典，按照开播时间排序
for i in range(1, 3):
    bangumi_params = {
        'season_version': '-1',
        'spoken_language_type': '-1',
        'area': '2',
        'is_finish': '-1',
        'copyright': '-1',
        'season_status': '-1',
        'season_month': '-1',
        'year': '-1',
        'style_id': '-1',
        'order': '5',
        'st': '1',
        'sort': '1',
        'page': f'{i}',
        'season_type': '1',
        'pagesize': '20',
        'type': '1'
    }
    # 有参数列表后可以将url之后的“参数列表”删去，会自动拼接
    url = "https://api.bilibili.com/pgc/season/index/result"
    res = requests.get(url, params=bangumi_params, headers=headers)
    # print(res.text)
    # 正则表达式：分组命名，其中.*?为怠惰匹配，即匹配到第一个结果就结束
    # 过滤的时候一定要注意，例如：[]、{}等不要都删了；在本例中，json是list对象，过筛的时候也应该保证list对象
    p = re.compile(r'"code":0,"data":\{"has_next":1,"list":(?P<jsondict>.*?),"num":'+str(i)+
                   ',"size":20,"total":3086\},"message":"success"')
    content = p.search(res.text).group('jsondict')
    # print(content)
    # 把json字典对象转换成python对象
    result_list = json.loads(content)
    # print(result_list[19])
    for dic in result_list:
        print(dic)
